package middleware

import (
	"bmember/pkg/mq"
	"context"
	"fmt"

	"github.com/nsqio/go-nsq"
)

// MqRecovery panic 恢复
func MqRecovery(handler mq.MqHandlerFunc) mq.MqHandlerFunc {
	return func(ctx context.Context, msg *nsq.Message) (err error) {
		defer func() {
			if rec := recover(); rec != nil {
				err = fmt.Errorf("%v", rec)
				sendMail(ctx, err, "MQ")
			}
		}()
		return handler(ctx, msg)
	}
}
